package com.walkline.screen;

import java.util.Hashtable;
import java.util.Vector;

import localization.WeiboSDKResource;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import com.walkline.util.Function;
import com.walkline.util.Enumerations.RefreshAction;
import com.walkline.util.container.ForegroundManager;
import com.walkline.util.container.MainMenuListStyleButtonSet;
import com.walkline.util.ui.MainMenuListStyleButtonField;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.GroupList;
import com.walkline.weibo.inf.GroupListWrapper;

public class ShowGroupListsMainScreen extends MainScreen implements WeiboSDKResource
{
	private static ResourceBundle _bundle = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private WeiboSDK _weibo;

	private ForegroundManager _foreground = new ForegroundManager(0);
	private MainMenuListStyleButtonSet _listSet = new MainMenuListStyleButtonSet();

	private String _page_count = "20";
	private String _current_page = "1";
	private String _max_id = "0";

	public ShowGroupListsMainScreen(WeiboSDK weibo, final GroupListWrapper groupLists)
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

		_weibo = weibo;
		//_groupLists = groupLIsts;

		HorizontalFieldManager hfm = new HorizontalFieldManager(FIELD_HCENTER);
		hfm.add(new LabelField("分组"));
		setTitle(hfm);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run() {refreshContents(groupLists);}
		});
	}

	private void refreshContents(GroupListWrapper groupListWrapper)
	{
		if (groupListWrapper == null)
		{
			Function.errorDialog("无法获取好友分组信息，请返回！");
			return;
		}

		Vector contents = groupListWrapper.getGroupLists();
		MainMenuListStyleButtonField item;

		if (groupListWrapper != null)
		{
			if (contents.size() > 0)
			{
				GroupList groupList = null;
				for (int i=0; i<contents.size(); i++)
				{
					groupList = (GroupList) contents.elementAt(i);
					item = new MainMenuListStyleButtonField(groupList);
					item.setChangeListener(new FieldChangeListener()
					{
						public void fieldChanged(Field field, int context)
						{
							MainMenuListStyleButtonField item = (MainMenuListStyleButtonField) _listSet.getFieldWithFocus();

							Hashtable params = new Hashtable();

							params.put("title", item.getMenuTitle());
							params.put("list_id", item.getGroupId());
							params.put("count", _page_count);
							params.put("page", _current_page);
							params.put("max_id", _max_id);

							UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.GROUPTIMELINE));
						}
					});
					_listSet.add(item);
				}
			}

			if (_foreground.getManager() == null) {add(_foreground);}
			if (_listSet.getManager() != _foreground) {_foreground.add(_listSet);}
		}
	}

	MenuItem menuRefreshMessage = new MenuItem(_bundle, MENU_REFRESH_MESSAGE, 100, 10)
    {
    	public void run() {}
    };

	protected void makeMenu(Menu menu, int instance)
	{
		super.makeMenu(menu, instance);
	};

	protected boolean keyChar(char character, int status, int time)
    {
    	switch (character)
    	{
			case Characters.LATIN_CAPITAL_LETTER_R:
			case Characters.LATIN_SMALL_LETTER_R:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_T:
			case Characters.LATIN_SMALL_LETTER_T:
				if (_listSet.getFieldCount() > 0) {_listSet.getField(0).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_B:
			case Characters.LATIN_SMALL_LETTER_B:
				if (_listSet.getFieldCount() > 0) {_listSet.getField(_listSet.getFieldCount()-1).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_Q:
			case Characters.LATIN_SMALL_LETTER_Q:
				Function.showExitDialog();
				return true;
		}

    	return super.keyChar(character, status, time);
    }
}